package com.ftg.learn.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 布衣
 */
@Configuration
@Slf4j
public class RedisManyResources {

    private final MyRedisProperties myRedisProperties;

    public RedisManyResources(MyRedisProperties myRedisProperties) {
        this.myRedisProperties = myRedisProperties;
    }

    //master
    @Bean
    public RedisTemplate<String, Object> redisMaster(){
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        MyRedisProperties.Node master = myRedisProperties.getMaster();
        LettuceConnectionFactory factory = new LettuceConnectionFactory(
                new RedisStandaloneConfiguration(master.getHost(), master.getPort()));
        factory.afterPropertiesSet();
        template.setConnectionFactory(factory);

        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());

        template.afterPropertiesSet();
        return template;
    }

    //follower templates
    @Bean
    public List<RedisTemplate<String, Object>> redisFollowers(){
        List<RedisTemplate<String, Object>> followers = new ArrayList<>();
        List<MyRedisProperties.Node> followerList = myRedisProperties.getFollower();
        for (int i = 0; i < followerList.size(); i++) {
            MyRedisProperties.Node follower = followerList.get(i);
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            LettuceConnectionFactory factory = new LettuceConnectionFactory(
                    new RedisStandaloneConfiguration(follower.getHost(), follower.getPort()));
            factory.afterPropertiesSet();
            template.setConnectionFactory(factory);
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new StringRedisSerializer());
            template.setHashKeySerializer(new StringRedisSerializer());
            template.setHashValueSerializer(new StringRedisSerializer());
            template.afterPropertiesSet();
            followers.add(template);
        }
        return followers;
    }

    // 负载均衡器
    @Bean
    public RedisFollowerLoadBalancer redisFollowerLoadBalancer() {
        return new RedisFollowerLoadBalancer(redisFollowers());
    }

    // 负载均衡读取模板
    @Bean
    public RedisTemplate<String, Object> redisFollowerBalanced() {
        return redisFollowerLoadBalancer().getBalancedFollower();
    }
}
